package com.sky.yipinyoufujava.controller;


import com.sky.yipinyoufujava.pojo.from.ServiceDateFrom;
import com.sky.yipinyoufujava.pojo.service.ServiceDate;
import com.sky.yipinyoufujava.pojo.service.ServiceTime;
import com.sky.yipinyoufujava.result.Result;
import com.sky.yipinyoufujava.service.ServiceDateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@RestController
@Slf4j
public class ServiceDateConteroller {

    @Autowired
    private ServiceDateService serviceDateService;

    @PostMapping("/getServiceDate")
    private Result getServiceDate(@RequestBody Integer shopsId){
//        添加一个字段，只获取今天往后的时间内容，
        LocalDate today = LocalDate.now();
        log.info("获取服务日期,{},{}",today,shopsId);
        List<ServiceDate> serviceDates = serviceDateService.getServiceDate(shopsId,today);
        // 设置title值
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        for (ServiceDate serviceDate : serviceDates) {
            LocalDate dateValue = LocalDate.parse(serviceDate.getValue(), formatter);
            log.info("当前的日期对比，{},{}",dateValue,today);
            if (dateValue.isEqual(today)) {
                serviceDate.setTitle("今天");
            } else if (dateValue.isEqual(today.plusDays(1))) {
                serviceDate.setTitle("明天");
            } else if (dateValue.isEqual(today.plusDays(2))) {
                serviceDate.setTitle("后天");
            }
        }
        return Result.success(serviceDates);
    };


    @PostMapping("/getServiceTime")
    private Result getServiceTime(@RequestBody Integer serviceId){
        // 获取当前的时分秒
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        String dateTimeNow = now.format(formatter);
        log.info("当前日期和时间：{}, {}", dateTimeNow, serviceId);
        List<ServiceTime> serviceDates = serviceDateService.getServiceTime(serviceId,dateTimeNow);
        return Result.success(serviceDates);
    };



//    获取店铺时间段的内容

    @PostMapping("/getShopsServiceTime")
    private Result getShopsServiceTime(@RequestBody ServiceDateFrom serviceDateFrom){
        log.info("获取店铺时间段的内容,{}",serviceDateFrom);
        LocalDateTime ldt = LocalDateTime.parse(serviceDateFrom.getDate(), DateTimeFormatter.ISO_DATE_TIME);
        serviceDateFrom.setDate(ldt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        ServiceDate serviceDates = serviceDateService.getShopsServiceDate(serviceDateFrom);
//        判断 serviceDates.getId() 是否存在，不存在直接返回空的数组
        if (serviceDates == null) {
//            先查询是否存在该时间段
            log.info("查询该时间段，{}",serviceDateFrom.getDate());
            Integer dateId =  serviceDateService.queryDateId(serviceDateFrom.getDate(),serviceDateFrom.getShopsId());
            if(dateId == null){
                log.info("不存在该时间段，需要添加，{}",serviceDateFrom.getDate());
                serviceDateService.addServiceDate(serviceDateFrom);
            }else{
                log.info("存在该时间段，{}",serviceDateFrom.getDate());
            }
            return Result.success(new ServiceTime[]{});
        }
        List<ServiceTime> serviceTimes = serviceDateService.getShopsServiceTime(serviceDates.getId());
        return Result.success(serviceTimes);
    };

//    添加时间段
    @PostMapping("/addServiceTime")
    private Result addServiceTime(@RequestBody ServiceDateFrom serviceDateFrom){
        log.info("添加时间段1,{}",serviceDateFrom);
//        通过日期查询获取到 id
        Integer dateId = serviceDateService.queryDateId(serviceDateFrom.getDate(),serviceDateFrom.getShopsId());
        serviceDateFrom.setServiceId(dateId);
        log.info("添加时间段3,{}",dateId);
        List<String> timeIntervals = generateTimeSlots(serviceDateFrom.getIntervalTime(),serviceDateFrom.getDate());
        log.info("添加时间段2,{}",timeIntervals);
        serviceDateService.addServiceTime(serviceDateFrom,timeIntervals);
        return Result.success("添加成功");
    };


    // 设置时间段，每间隔多久设置一个段
    public static List<String> generateTimeSlots(Integer interval,String date) {
        List<String> timeSlots = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            Calendar startTime = Calendar.getInstance();
            startTime.setTime(dateFormat.parse(date + " 08:00"));

            Calendar endTime = Calendar.getInstance();
            endTime.setTime(dateFormat.parse(date + " 23:00"));

            Calendar currentTime = (Calendar) startTime.clone();
            while (currentTime.before(endTime) || currentTime.equals(endTime)) {
                timeSlots.add(dateFormat.format(currentTime.getTime()));
                currentTime.add(Calendar.MINUTE, interval);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return timeSlots;
    }
}
